#
# myvector<-c("T","F","T","T","F","T")  #这里有一个向量
#
# myfactor<-factor(myvector)   #这是因子的用法
#
#
# x <- factor(c("low", "medium", "high", "medium", "low"))
#
# y <- ordered(c( "medium","low", "high"), levels = c("low", "medium", "high"))
#
#
# tapply(mydata1$age,mydata1$性别,mean)
#
# hight = c(175,167,174)
#
# mydata1 <- cbind(mydata1,hight)
#
# #tapply(x,f,g): x向量，f因子或因子列表，g函数。
# #tapply执行操作，将x分组，每组对应一个因子水平（多因子情况下对应一组水平的组合，然后向量应用于函数g）。
# #注意：f中每个因子需要与x具有相同的长度，返回值是向量或矩阵。x必须是向量
#
# tapply(mydata1$hight,list(mydata1$性别,mydata1$name),mean)
#
#
# #和tapply(x,f,g)不同split(x,f)只分组,x可为数据框或向量，返回值是列表。
# split(mydata1$name,mydata1$性别)  #对name按照性别进行分组，结果返回列表，标签是分组水平
#
# #by(x, f, function), x 向量或矩阵,注意by应用于对象，f 是因子，function 是函数。
# by(mydata1$higth,mydata1$性别,mean)
#
# aggregate(mydata1[,c(3,4)],list(mydata1$性别),mean)  #按性别聚合后，对age与higth进行求期望
#
# a<-c(1,2,3,4,2,3,1,1,1,3,3,3,2,4)
# table(a)
# #解释：1有4个，2有3个，3有5个，4有2个
#
# table(a)[3]  #看第三个
#
# list1<-list(c(2,2,2,3,4,5,5),c(6,6,7,7,7,8,8))
#
# table(list1)
#
# t1 <- table(list1[1])
# t2 <- table(list1[2])

#######################################################################################################

#cut(x, breaks, labels = NULL, right = TRUE, include.lowest = FALSE, ...)

heights <- c(150, 160, 170, 180, 190)

categories <- cut(heights, breaks = c(140, 160, 180, 200), labels = c("Short", "Average", "Tall"))
print(categories)

cat("\n")
# 左闭右开
categories <- cut(heights, breaks = c(140, 160, 180, 200), labels = c("Short", "Average", "Tall"), right = FALSE)
print(categories)

cat("\n")
# 左开右闭（默认）
categories <- cut(heights, breaks = c(140, 160, 180, 200), labels = c("Short", "Average", "Tall"), right = TRUE)
print(categories)

cat("\n")
# 包含最低值
categories <- cut(heights, breaks = c(140, 160, 180, 200), labels = c("Short", "Average", "Tall"), include.lowest = TRUE)
print(categories)


cat("\n")
#使用seq()生成分割点： 如果你想要生成等距的分割点，可以使用seq()函数。
categories <- cut(heights, breaks = seq(140, 200, by = 20), labels = c("Short", "Average", "Tall", "Very Tall"))
print(categories)

cat("\n")
#处理缺失值
heights <- c(150, NA, 170, 180, 190)
categories <- cut(heights, breaks = c(140, 160, 180, 200), labels = c("Short", "Average", "Tall"), na.rm = TRUE)
print(categories)
